package com.jackrain.nea.omslocal.util;

import com.alibaba.dubbo.common.utils.NetUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.aliyun.openservices.ons.api.impl.util.MsgConvertUtil;
import com.jackrain.nea.config.PropertiesConf;
import com.jackrain.nea.omslocal.Dao.*;
import com.jackrain.nea.omslocal.service.SaveSkuErrservice;
import com.jackrain.nea.omslocal.service.SavelogisticMsgService;
import com.jackrain.nea.omslocal.service.SendMqServies;
import com.jackrain.nea.util.ApplicationContextHandle;
import lombok.extern.slf4j.Slf4j;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;

/**
 * Description: 商品,退单,分销订单,经销订单,分销退单的监听
 * Created with IntelliJ IDEA.
 * User: cs
 * Date: 2017/3/29
 * Time: 18:37
 */
@Slf4j
public class OMSMessageListener implements MessageListener {


    public OMSMessageListener() {

    }

    @Override
    public Action consume(Message message, ConsumeContext consumeContext) {

        SendMqServies sendMq = ApplicationContextHandle.getBean(SendMqServies.class);
        PropertiesConf propertiesConf = ApplicationContextHandle.getBean(PropertiesConf.class);
        SaveSkuErrservice saveSkuErrservice = ApplicationContextHandle.getBean(SaveSkuErrservice.class);
        TaoBaoMemberDao taoBaoMemberDao = ApplicationContextHandle.getBean(TaoBaoMemberDao.class);
        SavelogisticMsgService savelogisticMsgService = ApplicationContextHandle.getBean(SavelogisticMsgService.class);
        // 获取 流
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        try {

            if (message.getTopic().equals(propertiesConf.getProperty("ons.menberTopic"))) {
                // 店铺会员下载
                JSONObject jsonObject = JSONObject.parseObject(MsgConvertUtil.objectDeserialize(message.getBody()).toString());
                taoBaoMemberDao.memberStart(jsonObject, message.getKey(), message.getTopic());
                log.debug(message.getTopic() + message.getTag());

            } else if (message.getTopic().equals(propertiesConf.getProperty("ons.qtyErrTopic"))&&message.getTag().equals(propertiesConf.getProperty("ons.comp"))) {
                // 同步库存异常处理回写
                JSONArray qtyErrArr = JSONArray.parseArray(MsgConvertUtil.objectDeserialize(message.getBody()).toString());
                saveSkuErrservice.saveSkuerr(qtyErrArr);

            } else if (message.getTopic().equals(propertiesConf.getProperty("ons.logisiceTopic"))) {
                // 同步发货回执回写
                JSONArray logisticArr = JSONArray.parseArray(MsgConvertUtil.objectDeserialize(message.getBody()).toString());
                savelogisticMsgService.saveLogisic(logisticArr,message.getKey());

            } else {
                //log.error("没有对应的消费topic,消费失败");
                return Action.ReconsumeLater;
            }

        } catch (Exception e) {
            log.error("消费报错========");
//            // 逐条将错误信息以消息发送到线上发送到线上
//            JSONObject errObj = new JSONObject();
//            errObj.put("flag",0);
//            errObj.put("topic", message.getTopic());
//            errObj.put("tag", message.getTag());
//            errObj.put("msgkey", message.getKey());
//            // 线下消费报错的主机 ip
//            errObj.put("ip", NetUtils.getLocalAddress());
//            // 将错误以流写出
//            e.printStackTrace(pw);
//            errObj.put("errmsg", sw.toString());
//            errObj.put("sendtime", new Date());
//            try {
//                sendMq.sendMqObj(errObj, propertiesConf.getProperty("ons.jdLogErrTopic"), message.getTag());
//            } catch (Exception e1) {
//                e1.printStackTrace();
//                log.debug("京东库存同步发送错误消息失败");
//            }
            e.printStackTrace();
            // 出现异常,再次消费
            return Action.ReconsumeLater;
        }

        return Action.CommitMessage;
    }
}